Announcement

Collapse
No announcement yet.
X
  • Filter
  • Time
  • Show
Clear All
new posts

  • Parsing vector of parameters in Mata using Stata's `ml` command

    I want to parse the parameter vector passed through the `ml` command to a Mata evaluator.

    To make things concrete, I have a model with 4 regressors in which 1, one of them is fixed, and the other 3 are random. Additionally, each random effect can have a polynomial expansion (let's imagine a Taylor expansion).

    For the example, take this data (not really relevant)


    Code:
    clear all
    input id_ind    id_cs_of_ind_n    id_cs    alternative    x1    x2    x3 x_fix y 
    1    1    1    1    -2.4 .08 0.5    0.5 1
    1    1    1    2    1.1     -1.8 0.65    0.15 0
    end

    Now here are the macros that describe the problem. As we can see below, the first (`x1`), second (`x2`), and the third variable (`x3`) have polynomial expansions of order 1, 2, and 3, respectively. This information is contained in the macro `poly`.


    Code:
    // Macros available 
    local fixed "x_fix"
    local rand "x1 x2 x3"
    local poly "1 2 3" 
    local lhs "y"
    local rhs `fixed' `rand' 
    local kfix: word count `fixed'
    local krnd: word count `rand'
    
    mata: mata_kfix = strtoreal(st_local("kfix"))
    mata: mata_krnd = strtoreal(st_local("krnd"))
    Now I will use the ml command to call a mata evaluator (`eval_gf0`) (see below). I added the extra terms using the `/` expression. So you can see, for example, that `x3_3` represents the parameter of the third polynomial expansion of the random variable `x3`.


    Code:
    ml model gf0 eval_gf0()  (eq0: y = x_fix x1 x2 x3, noconst) /x2_2 /x3_2 /x3_3 
    
    matrix init = (1,2,3,4,5,6,7) 
    mat li init
    ml init init ,copy
    ml maximize  ,iter(0)
    Here is the evaluator `eval_gf0` together with the desired objects.


    Code:
    // Evaluator used by the ml command.
    mata:
    void eval_gf0(/*string scalar B_s*/
      transmorphic scalar MM, real scalar todo, 
      real rowvector b, real colvector  lnfj, 
      S, H)
    {
    
    external mata_kfix
    external mata_krnd
    
    kfix = mata_kfix
    krnd = mata_krnd
    
    // Transposing the original vector into a column row.
    B = b' 
    
    // Total number of variables in equation 0
    keq0 = kfix + krnd 
    
    /* This recover the fixed parameter */
    MFIX = B[|1,1\kfix,1|]
    
    /*
      MFIX
      1
    */
    
    /* This recovers the first term in the polynomial expansion for the three random variables. */
    MPOLY_0 = B[|(kfix+1),1 \ keq0,1|]
    /*
    MPOLY_0
           1
        +-----+
      1 |  2  |
      2 |  3  |
      3 |  4  |
        +-----+
    
    */
    
    /*
    
     Here I would like to create one matrix per polynomial order 
     and fill the missing spots with zeros when a given variable 
     does not have a polynomial of this order.
    
     For example, in this case, I should get the following: 
    
    MPOLY_1  
    
             1
          +-----+ 
        1 |  0  | <-  this 0 is because there is polynomial of order 2 for x1
        2 |  5  |
        3 |  6  |  
          +-----+
    
          
    MPOLY_2 
     
             1
          +-----+ 
        1 |  0  | <-  this 0 is because there is polynomial of order 3 for x1
        2 |  0  | <-  this 0 is because there is polynomial of order 3 for x2
        3 |  7  |
          +-----+
      
    */
    
    exit(777) 
    
    }
    end
    Any idea or hint about how to perform such operations? Thank you in advance.

    PS1: When replicating this into Stata you need to put the evaluator BEFORE invoking it using the `ml` command.

    PS2: Crossposted at Stackoverflow
Working...
X